
// 用户名
let username = document.getElementById('username')
let usermsg = document.getElementById('usermsg')
// 手机
let userphone = document.getElementById('userphone')
let phonemsg = document.getElementById('phonemsg')
// 密码
let userpass = document.getElementById('userpass')
let passmsg = document.getElementById('passmsg')
// 确认密码
let userpass2 = document.getElementById('userpass2')
let passmsg2 = document.getElementById('passmsg2')
// 登录
let btnreg = document.getElementById('btnreg')

let flageuserName = false
function getusername() {

    var userNamePattern = /^[a-zA-Z0-9_-]{4,16}$/
    if (userNamePattern.test(username.value)) {
        console.log('ok');
        usermsg.innerHTML = '√';
        usermsg.style.color = 'green'
        flageuserName = true
    } else {
        console.log('no');
        usermsg.innerHTML = '用户名应由 4到16位(字母、数字、下划线)组成';
        usermsg.style.color = 'red';
        usermsg.style.fontSize = '12px';
        flageuserName = false
    }
}

let flageiphone = false
function userphonePattern() {
    // 手机号码正则
    // 手机号： 15  13  18 开头
    let reg = /^(13|15|18)\d{9}$/;
    if (reg.test(userphone.value)) {
        phonemsg.innerHTML = '√';
        phonemsg.style.color = 'green'
        flageiphone = true
    } else {
        phonemsg.innerHTML = '手机号码格式不对';
        phonemsg.style.color = 'red';
        phonemsg.style.fontSize = '12px';
        flageiphone = false
    }
}

let flagePwd = false;
function userpassPattertn() {
    // 密码：6-18位字符组成       有密码强弱验证 
    //数字
    var regNum = /\d+/;
    //字母
    var regChar = /[a-zA-Z]+/;
    //其他字符
    var regLetter = /[!@#$]+/;

    //只能
    //数字
    var _regNum = /^\d+$/;
    //字母
    var _regChar = /^[a-zA-Z]+$/;
    //其他字符
    var _regLetter = /^[!@#$]+$/;
    let regs = /^\w{6,18}$/;
    if (!regs.test(userpass.value)) {
        passmsg.innerHTML = '密码6-18位字符组成'
        passmsg.style.color = 'red'
        passmsg.style.fontSize = '12px'
        flagePwd = false
    } else {
        passmsg.innerHTML = ''
        flagePwd = true
        if (regNum.test(this.value) && regChar.test(this.value)
            && regLetter.test(this.value)) {
            passmsg.innerHTML = '强'
            passmsg.style.color = 'green'
        } else if (_regNum.test(this.value) || _regChar.test(this.value)
            || _regLetter.test(this.value)) {
            passmsg.innerHTML = '弱'
            passmsg.style.color = 'pink'
        } else {
            passmsg.innerHTML = '中'
            passmsg.style.color = 'orange'

        }
    }
}


let flagePwd1 = false;
function pass2Pattertn() {
    // 确认密码和密码相同
    if (userpass.value != userpass2.value) {
        passmsg2.innerHTML = '确认密码和密码不相同,请检查'
        passmsg2.style.color = 'red'
        passmsg2.style.fontSize = '12px';
        flagePwd1 = false
    } else if (userpass.value === '' && userpass2.value === '') {
        passmsg2.innerHTML = '确认密码和密码不能为空'
        passmsg2.style.color = 'red'
        passmsg2.style.fontSize = '12px';
        flagePwd1 = false
    } else {
        passmsg2.innerHTML = '√'
        passmsg2.style.color = 'green'
        flagePwd1 = true
    }
}

function putdata() {
    ajaxUsePromise02({
        url: `http://localhost:3000/vips`,
        method: "post",
        data: `username=${username.value}&userphone=${userphone.value}&userpass=${userpass.value}&userpass2=${userpass2.value}`
    }).then(res => {
        // console.log("res", res);

    })
}

window.onload = function () {
    username.onblur = function () {
        getusername();
    }
    userphone.onblur = function () {
        userphonePattern()
    }
    userpass.onblur = function () {
        userpassPattertn()
    }
    userpass2.onblur = function () {
        pass2Pattertn()
    }

    btnreg.onclick = function () {

        // if (flageuserName && flageiphone && flageiphone && flagePwd1) {
        putdata()
        // alert('注册成功，点击确定1s后进入登录页')
        // setInterval(function () {
        location.href = './login.html'
        // }, 1000)
        // return true
        // } else {
        // alert('shi')
        // return false
        // }
    }
}